//
// Created by wangjunjun on 2024/2/6.
//
#include "KeyBoardManger.h"

#include <qdebug.h>
#include <QApplication>
#include <QDateTime>
#include <QCryptographicHash>
#include "src/util/crc/crc.h"
KeyBoardManger::KeyBoardManger(QObject *parent)
        : QObject{parent}
{
    installEventFilter(this);
    QApplication::instance()->installEventFilter(this);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &KeyBoardManger::countKeys);
}

void KeyBoardManger::onCountKeysMsg()
{
    QString udKeys;  //+代表向前， -代表向后
    QString rfKeys;  //+代表向左， -代表后右
    QString keyMsg;
    double uddiff = static_cast<double>(upCount - downCount)*0.1;
    udKeys = (uddiff == 0.0) ? "0" : ((uddiff > 0.0) ? "+" : "") + QString::number(uddiff, 'f', 2);
    double rfdiff = static_cast<double>(leftCount - rightCount)*0.1;
    rfKeys = (rfdiff == 0.0) ? "0" : ((rfdiff > 0.0) ? "+" : "") + QString::number(rfdiff, 'f', 2);
    keyMsg= udKeys + " " + rfKeys  + " " + QString::number(QDateTime::currentMSecsSinceEpoch());
    //添加CRC校验
    QString crc = CalculatedCRC::GetInstance().calculateCRC32(keyMsg);
    QString dataWithCRC = keyMsg +" "+ crc;
    emit SenKeyMsg(dataWithCRC);

}
//param：统计按键按下次数
void KeyBoardManger::countKeys()
{
    upCount +=QApplication::queryKeyboardModifiers()& Qt::Key_Up?1:0;
    downCount+= QApplication::queryKeyboardModifiers()& Qt::Key_Down?1:0;
    leftCount+=QApplication::queryKeyboardModifiers()& Qt::Key_Left?1:0;
    rightCount+=QApplication::queryKeyboardModifiers()& Qt::Key_Right?1:0;
    onCountKeysMsg();
}

bool KeyBoardManger::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        // 根据按下的键启动定时器，开始计数
        switch (keyEvent->key())
        {
            case Qt::Key_Up:
                if (!keyEvent->isAutoRepeat() && keyEvent->key() == Qt::Key_Up) {
                    if (!timer->isActive()) {
                        timer->start(10); // 100 milliseconds interval for counting
                        upCount = 1;
                    }
                }
                if(keyEvent->isAutoRepeat() && keyEvent->key() == Qt::Key_Up){
                    if (!timer->isActive()){
                        timer->start(10);
                    }
                    upCount++;
                    if(upCount  > Speed_Max){
                        upCount = Speed_Max;
                    }
                }
                break;
            case Qt::Key_Down:
                if (!keyEvent->isAutoRepeat() && keyEvent->key() ==  Qt::Key_Down) {
                    if (!timer->isActive()) {
                        timer->start(10);
                        downCount = 1;
                    }
                }
                if (keyEvent->isAutoRepeat() && keyEvent->key() == Qt::Key_Down){
                    if (!timer->isActive()){
                        timer->start(10);
                    }
                    downCount++;
                    if( downCount  > Speed_Max){
                        downCount = Speed_Max;
                    }
                }
                break;
            case Qt::Key_Left:
                if (!keyEvent->isAutoRepeat()  && keyEvent->key() == Qt::Key_Left) {
                    if (!timer->isActive()) {
                        timer->start(10);
                        leftCount = 1;
                    }
                }
                if (keyEvent->isAutoRepeat() && keyEvent->key() == Qt::Key_Left){
                    if (!timer->isActive()){
                        timer->start(10);
                    }
                    leftCount++;
                    if( leftCount  > Speed_Max){
                        leftCount = Speed_Max;
                    }
                }
                break;
            case Qt::Key_Right:
                if (!keyEvent->isAutoRepeat() && keyEvent->key() == Qt::Key_Right) {
                    if (!timer->isActive()) {
                        timer->start(10);
                        rightCount= 1;
                    }
                }
                if (keyEvent->isAutoRepeat() && keyEvent->key() == Qt::Key_Right){
                    if (!timer->isActive()){
                        timer->start(10);
                    }
                    rightCount++;
                    if( rightCount  > Speed_Max){
                        rightCount = Speed_Max;
                    }
                }
                break;
            default:
                break;
        }
    }
    else if (event->type() == QEvent::KeyRelease)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        // 根据松开的键停止定时器，并将计数归零
        switch (keyEvent->key())
        {
            case Qt::Key_Up:
                if (!keyEvent->isAutoRepeat()) {
                    if (timer->isActive()) {
                        upCount = 0;
                    }
                }
                break;
            case Qt::Key_Down:
                if (!keyEvent->isAutoRepeat()) {
                    if (timer->isActive()) {
                        downCount = 0;
                    }
                }
                break;
            case Qt::Key_Left:
                if (!keyEvent->isAutoRepeat()) {
                    if (timer->isActive()) {
                        leftCount = 0;
                    }
                }
                break;
            case Qt::Key_Right:
                if (!keyEvent->isAutoRepeat()) {
                    if (timer->isActive()) {
                    rightCount = 0;
                    }
        }
            default:
                break;
        }

        // 如果所有按键都松开了，则停止定时器
        if (upCount == 0 && downCount == 0 && leftCount == 0 && rightCount == 0) {
            if (timer->isActive()) {
                countKeys();
                timer->stop();
            }
        }
    }
    return QObject::eventFilter(obj, event);
}
